/*
 * OcchoView.java
 */

package occho.view;

import java.awt.Color;
import java.awt.Component;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Locale;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import occho.OcchoApp;
import occho.data.LogDAO;
import occho.data.OracleDAOFactory;
import occho.data.UsuarioDAO;
import occho.model.Log;
import occho.model.Usuario;
import occho.view.resources.Messages;

/**
 * The application's main frame.
 */
public class OcchoView extends FrameView implements Internacionalizable {

    /**
     * Vista principal
     * @param app
     */
    public OcchoView(SingleFrameApplication app) {
        super(app);

        initComponents();
        // Internacionalizacion
        locale = Locale.getDefault();
        Messages.setLocale(locale);
        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
        
        modificarVista();
        
        //addPropertyChangeListener(new );
    }

    /**
     * Mostrar vista.
     */
    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = OcchoApp.getApplication().getMainFrame();
            aboutBox = new OcchoAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        OcchoApp.getApplication().show(aboutBox);
    }

    /**
     * Establce userApp
     * @param user
     */
    public void setUserApp(Usuario user){
        OcchoApp.getApplication().setUsuario(user);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        mdiPanel = new javax.swing.JDesktopPane();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        loginMenuItem = new javax.swing.JMenuItem();
        registrarMenuItem = new javax.swing.JMenuItem();
        logoutSeparator = new javax.swing.JPopupMenu.Separator();
        logoutMenuItem = new javax.swing.JMenuItem();
        exitSeparator = new javax.swing.JPopupMenu.Separator();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        administradorMenu = new javax.swing.JMenu();
        profesoresMenuItem = new javax.swing.JMenuItem();
        alumnosMenuItem = new javax.swing.JMenuItem();
        logMenuItem = new javax.swing.JMenuItem();
        profesoresMenu = new javax.swing.JMenu();
        asignaturasMenuItem = new javax.swing.JMenuItem();
        crearTestMenuItem = new javax.swing.JMenuItem();
        borrarTestsMenuItem = new javax.swing.JMenuItem();
        verResultadosProfesorMenuItem = new javax.swing.JMenuItem();
        alumnosMenu = new javax.swing.JMenu();
        realizarTestMenuItem = new javax.swing.JMenuItem();
        verResultadosMenuItem = new javax.swing.JMenuItem();
        lenguajesMenu = new javax.swing.JMenu();
        spanishMenuItem = new javax.swing.JMenuItem();
        englishMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.setPreferredSize(new java.awt.Dimension(640, 480));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(occho.OcchoApp.class).getContext().getResourceMap(OcchoView.class);
        mdiPanel.setBackground(resourceMap.getColor("mdiPanel.background")); // NOI18N
        mdiPanel.setName("mdiPanel"); // NOI18N

        org.jdesktop.layout.GroupLayout mainPanelLayout = new org.jdesktop.layout.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mdiPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 701, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mdiPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 474, Short.MAX_VALUE)
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        loginMenuItem.setText(resourceMap.getString("loginMenuItem.text")); // NOI18N
        loginMenuItem.setName("loginMenuItem"); // NOI18N
        loginMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(loginMenuItem);

        registrarMenuItem.setText(resourceMap.getString("registrarMenuItem.text")); // NOI18N
        registrarMenuItem.setName("registrarMenuItem"); // NOI18N
        registrarMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                registrarMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(registrarMenuItem);

        logoutSeparator.setName("logoutSeparator"); // NOI18N
        fileMenu.add(logoutSeparator);

        logoutMenuItem.setText(resourceMap.getString("logoutMenuItem.text")); // NOI18N
        logoutMenuItem.setEnabled(false);
        logoutMenuItem.setName("logoutMenuItem"); // NOI18N
        logoutMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logoutMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(logoutMenuItem);

        exitSeparator.setName("exitSeparator"); // NOI18N
        fileMenu.add(exitSeparator);

        exitMenuItem.setText(resourceMap.getString("exitMenuItem.text")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        administradorMenu.setText(resourceMap.getString("administradorMenu.text")); // NOI18N
        administradorMenu.setEnabled(false);
        administradorMenu.setName("administradorMenu"); // NOI18N

        profesoresMenuItem.setText(resourceMap.getString("profesoresMenuItem.text")); // NOI18N
        profesoresMenuItem.setName("profesoresMenuItem"); // NOI18N
        profesoresMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                profesoresMenuItemActionPerformed(evt);
            }
        });
        administradorMenu.add(profesoresMenuItem);

        alumnosMenuItem.setText(resourceMap.getString("alumnosMenuItem.text")); // NOI18N
        alumnosMenuItem.setName("alumnosMenuItem"); // NOI18N
        alumnosMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                alumnosMenuItemActionPerformed(evt);
            }
        });
        administradorMenu.add(alumnosMenuItem);

        logMenuItem.setText(resourceMap.getString("logMenuItem.text")); // NOI18N
        logMenuItem.setName("logMenuItem"); // NOI18N
        logMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logMenuItemActionPerformed(evt);
            }
        });
        administradorMenu.add(logMenuItem);

        menuBar.add(administradorMenu);

        profesoresMenu.setText(resourceMap.getString("profesoresMenu.text")); // NOI18N
        profesoresMenu.setEnabled(false);
        profesoresMenu.setName("profesoresMenu"); // NOI18N

        asignaturasMenuItem.setText(resourceMap.getString("asignaturasMenuItem.text")); // NOI18N
        asignaturasMenuItem.setName("asignaturasMenuItem"); // NOI18N
        asignaturasMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                asignaturasMenuItemActionPerformed(evt);
            }
        });
        profesoresMenu.add(asignaturasMenuItem);

        crearTestMenuItem.setText(resourceMap.getString("crearTestMenuItem.text")); // NOI18N
        crearTestMenuItem.setName("crearTestMenuItem"); // NOI18N
        crearTestMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                crearTestMenuItemActionPerformed(evt);
            }
        });
        profesoresMenu.add(crearTestMenuItem);

        borrarTestsMenuItem.setText(resourceMap.getString("borrarTestsMenuItem.text")); // NOI18N
        borrarTestsMenuItem.setName("borrarTestsMenuItem"); // NOI18N
        borrarTestsMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                borrarTestsMenuItemActionPerformed(evt);
            }
        });
        profesoresMenu.add(borrarTestsMenuItem);

        verResultadosProfesorMenuItem.setText(resourceMap.getString("verResultadosProfesorMenuItem.text")); // NOI18N
        verResultadosProfesorMenuItem.setName("verResultadosProfesorMenuItem"); // NOI18N
        verResultadosProfesorMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                verResultadosProfesorMenuItemActionPerformed(evt);
            }
        });
        profesoresMenu.add(verResultadosProfesorMenuItem);

        menuBar.add(profesoresMenu);

        alumnosMenu.setText(resourceMap.getString("alumnosMenu.text")); // NOI18N
        alumnosMenu.setEnabled(false);
        alumnosMenu.setName("alumnosMenu"); // NOI18N

        realizarTestMenuItem.setText(resourceMap.getString("realizarTestMenuItem.text")); // NOI18N
        realizarTestMenuItem.setName("realizarTestMenuItem"); // NOI18N
        realizarTestMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                realizarTestMenuItemActionPerformed(evt);
            }
        });
        alumnosMenu.add(realizarTestMenuItem);

        verResultadosMenuItem.setText(resourceMap.getString("verResultadosMenuItem.text")); // NOI18N
        verResultadosMenuItem.setName("verResultadosMenuItem"); // NOI18N
        verResultadosMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                verResultadosMenuItemActionPerformed(evt);
            }
        });
        alumnosMenu.add(verResultadosMenuItem);

        menuBar.add(alumnosMenu);

        lenguajesMenu.setText(resourceMap.getString("lenguajesMenu.text")); // NOI18N
        lenguajesMenu.setName("lenguajesMenu"); // NOI18N

        spanishMenuItem.setIcon(resourceMap.getIcon("spanishMenuItem.icon")); // NOI18N
        spanishMenuItem.setText(resourceMap.getString("spanishMenuItem.text")); // NOI18N
        spanishMenuItem.setName("spanishMenuItem"); // NOI18N
        spanishMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                spanishMenuItemActionPerformed(evt);
            }
        });
        lenguajesMenu.add(spanishMenuItem);

        englishMenuItem.setIcon(resourceMap.getIcon("englishMenuItem.icon")); // NOI18N
        englishMenuItem.setText(resourceMap.getString("englishMenuItem.text")); // NOI18N
        englishMenuItem.setName("englishMenuItem"); // NOI18N
        englishMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                englishMenuItemActionPerformed(evt);
            }
        });
        lenguajesMenu.add(englishMenuItem);

        menuBar.add(lenguajesMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(occho.OcchoApp.class).getContext().getActionMap(OcchoView.class, this);
        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        org.jdesktop.layout.GroupLayout statusPanelLayout = new org.jdesktop.layout.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(statusPanelSeparator, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 701, Short.MAX_VALUE)
            .add(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(statusMessageLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 531, Short.MAX_VALUE)
                .add(progressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(statusPanelLayout.createSequentialGroup()
                .add(statusPanelSeparator, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(statusMessageLabel)
                    .add(statusAnimationLabel)
                    .add(progressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

private void loginMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginMenuItemActionPerformed
    if (mdiPanel.getComponentCount() == 0) {
        LoginFrame loginFrame = new LoginFrame(this);
        addFrame(loginFrame);     
    }
}//GEN-LAST:event_loginMenuItemActionPerformed

private void logoutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logoutMenuItemActionPerformed
        try {
            OracleDAOFactory daoFactory = OracleDAOFactory.getInstancia();
            LogDAO lDAO = daoFactory.getLogDAO();
            Log l = new Log(getUsuario(),new Timestamp(Calendar.getInstance().getTimeInMillis()),"Logout");
            if(lDAO.insertarLog(l)) setStatusMessageLabel("Creada entrada en log",Color.BLACK); else setStatusMessageLabel("Error al crear entrada en log",Color.RED);
            OracleDAOFactory factory = OracleDAOFactory.getInstancia();
            UsuarioDAO uDAO =factory.getUsuarioDAO();
            uDAO.setActivo(usuario.getEmail(), false);
            setUsuario(null);
            for (Component c : mdiPanel.getComponents()) {
                c.setVisible(false);
            }
            mdiPanel.removeAll();
        } catch (ClassNotFoundException ex) {}
          catch (SQLException ex) {}
}//GEN-LAST:event_logoutMenuItemActionPerformed

private void registrarMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_registrarMenuItemActionPerformed
    if (mdiPanel.getComponentCount() == 0) {
        RegistroFrame registroFrame = new RegistroFrame(this);
        addFrame(registroFrame);
    }
}//GEN-LAST:event_registrarMenuItemActionPerformed

private void profesoresMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_profesoresMenuItemActionPerformed
    if (mdiPanel.getComponentCount() == 0) {
        ProfesoresFrame profesoresFrame = new ProfesoresFrame(usuario);
        addFrame(profesoresFrame);
    }
}//GEN-LAST:event_profesoresMenuItemActionPerformed

private void asignaturasMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_asignaturasMenuItemActionPerformed
    if (mdiPanel.getComponentCount() == 0) {
        AsignaturasFrame asignaturasFrame = new AsignaturasFrame(this.usuario);
        addFrame(asignaturasFrame);
    }
}//GEN-LAST:event_asignaturasMenuItemActionPerformed

private void realizarTestMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_realizarTestMenuItemActionPerformed
    boolean este=false;
    for(Component c: mdiPanel.getComponents()){
        if(c instanceof RealizarTestFrame)
            este=true;
    }
    if(!este){
        RealizarTestFrame realizarTestFrame = null;

        try {
            realizarTestFrame = new RealizarTestFrame(this);
        } catch (SQLException ex) {
            Logger.getLogger(OcchoView.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(OcchoView.class.getName()).log(Level.SEVERE, null, ex);
        }

        addFrame(realizarTestFrame);
    }
}//GEN-LAST:event_realizarTestMenuItemActionPerformed

    private void alumnosMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_alumnosMenuItemActionPerformed
        AlumnosFrame alumnosFrame = new AlumnosFrame(usuario);
        addFrame(alumnosFrame);
    }//GEN-LAST:event_alumnosMenuItemActionPerformed

private void crearTestMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_crearTestMenuItemActionPerformed
    if (mdiPanel.getComponentCount() == 0) {
        CrearTestFrame testFrame = new CrearTestFrame(this);
        addFrame(testFrame);
    }
}//GEN-LAST:event_crearTestMenuItemActionPerformed

private void verResultadosMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_verResultadosMenuItemActionPerformed
    boolean este=false;
    for(Component c: mdiPanel.getComponents()){
        if(c instanceof ResultadosAlumnoFrame)
            este=true;
    }
    if(!este){
        ResultadosAlumnoFrame resultadosFrame = new ResultadosAlumnoFrame(this.usuario);
        addFrame(resultadosFrame);
    }
}//GEN-LAST:event_verResultadosMenuItemActionPerformed

    private void borrarTestsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_borrarTestsMenuItemActionPerformed
        if (mdiPanel.getComponentCount() == 0) {
            BorrarTestsFrame testFrame = new BorrarTestsFrame(this);
            addFrame(testFrame);
        }
    }//GEN-LAST:event_borrarTestsMenuItemActionPerformed

    private void logMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logMenuItemActionPerformed
        LogFrame logFrame = new LogFrame(this);
        addFrame(logFrame);    
    }//GEN-LAST:event_logMenuItemActionPerformed

private void spanishMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_spanishMenuItemActionPerformed
    locale = Locale.getDefault();
    Messages.setLocale(locale);
    setLenguaje();
}//GEN-LAST:event_spanishMenuItemActionPerformed

private void englishMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_englishMenuItemActionPerformed
    locale = new Locale("en", "US");
    Messages.setLocale(locale);
    setLenguaje();
}//GEN-LAST:event_englishMenuItemActionPerformed

    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
        try {
            if(usuario != null){
                OracleDAOFactory factory = OracleDAOFactory.getInstancia();
                UsuarioDAO uDAO =factory.getUsuarioDAO();
                uDAO.setActivo(usuario.getEmail(), false);
            }
            OcchoApp.getApplication().exit();
        } catch (ClassNotFoundException ex) {}
          catch (SQLException ex) {}
        
    }//GEN-LAST:event_exitMenuItemActionPerformed

private void verResultadosProfesorMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_verResultadosProfesorMenuItemActionPerformed
    if (mdiPanel.getComponentCount() == 0) {
        ResultadosProfesorFrame resultadosFrame = new ResultadosProfesorFrame(this.usuario);
        addFrame(resultadosFrame);
    }
}//GEN-LAST:event_verResultadosProfesorMenuItemActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu administradorMenu;
    private javax.swing.JMenu alumnosMenu;
    private javax.swing.JMenuItem alumnosMenuItem;
    private javax.swing.JMenuItem asignaturasMenuItem;
    private javax.swing.JMenuItem borrarTestsMenuItem;
    private javax.swing.JMenuItem crearTestMenuItem;
    private javax.swing.JMenuItem englishMenuItem;
    private javax.swing.JPopupMenu.Separator exitSeparator;
    private javax.swing.JMenu lenguajesMenu;
    private javax.swing.JMenuItem logMenuItem;
    private javax.swing.JMenuItem loginMenuItem;
    private javax.swing.JMenuItem logoutMenuItem;
    private javax.swing.JPopupMenu.Separator logoutSeparator;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JDesktopPane mdiPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenu profesoresMenu;
    private javax.swing.JMenuItem profesoresMenuItem;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JMenuItem realizarTestMenuItem;
    private javax.swing.JMenuItem registrarMenuItem;
    private javax.swing.JMenuItem spanishMenuItem;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JMenuItem verResultadosMenuItem;
    private javax.swing.JMenuItem verResultadosProfesorMenuItem;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
    private Locale locale;

    private JDialog aboutBox;
    
    // Atributo Usuario
    private Usuario usuario;

    /**
     * @return the usuario
     */
    public Usuario getUsuario() {
        return usuario;
    }

    /**
     * @param usuario the usuario to set
     */
    public void setUsuario(Usuario usuario) {
        this.usuario = usuario;
        modificarVista();
    }
    
    /**
     * @return the statusMessageLabel
     */
    public javax.swing.JLabel getStatusMessageLabel() {
        return statusMessageLabel;
    }
    
    /**
     * Establecer mensaje.
     * @param m
     * @param c
     */
    public void setStatusMessageLabel(String m, Color c) {
        this.statusMessageLabel.setText(m);
        this.statusMessageLabel.setForeground(c);
    }
    
    /**
     * Establecer mensaje
     * @param m
     */
    public void setStatusMessageLabel(String m) {
        this.statusMessageLabel.setText(m);
    }    
    
    /**
     * Añadir frame
     * @param jif
     */
    public void addFrame(JInternalFrame jif) {
        mdiPanel.add(jif);
        if (mdiPanel.getWidth() > jif.getWidth() && mdiPanel.getHeight() > jif.getHeight()) {
            jif.setLocation(mdiPanel.getWidth()/2 - jif.getWidth()/2, 
                mdiPanel.getHeight()/2 - jif.getHeight()/2);
        }
        jif.setVisible(true);
    }
    
    /**
     * Muestra los botones dependiendo del tipo de usuario logeado
     */
    private void modificarVista() {
        if (usuario != null) {
            loginMenuItem.setEnabled(false);
            registrarMenuItem.setEnabled(false);
            logoutMenuItem.setEnabled(true);
            
            String tipo = usuario.getTipo();
            // Activamos las opciones segun el tipo de usuario
            if (tipo.equals(Usuario.ADMIN)) {
                setOpcionesAdmin(true);
                setOpcionesProfesor(false);
                setOpcionesAlumno(false);
            } 
            else if (tipo.equals(Usuario.PROFESOR)) {
                setOpcionesAdmin(false);
                setOpcionesProfesor(true);
                setOpcionesAlumno(false);
            } 
            else {
                setOpcionesAdmin(false);
                setOpcionesProfesor(false);
                setOpcionesAlumno(true);
            }
            
            this.setStatusMessageLabel("Sesión iniciada como " + usuario.toString(),Color.BLACK);
        }
        else {
            loginMenuItem.setEnabled(true);
            registrarMenuItem.setEnabled(true);
            logoutMenuItem.setEnabled(false);
            
            setOpcionesAdmin(false);
            setOpcionesProfesor(false);
            setOpcionesAlumno(false);
            
            this.setStatusMessageLabel("Sesión no iniciada",Color.RED);
        }
    }
    
    public void setLenguaje() {
        administradorMenu.setText(Messages.getString("administradorMenu.text", Messages.BUNDLE_NAME_OCCHO_VIEW));
        alumnosMenu.setText(Messages.getString("alumnosMenu.text", Messages.BUNDLE_NAME_OCCHO_VIEW));
        alumnosMenuItem.setText(Messages.getString("alumnosMenuItem.text", Messages.BUNDLE_NAME_OCCHO_VIEW));
        asignaturasMenuItem.setText(Messages.getString("asignaturasMenuItem.text", Messages.BUNDLE_NAME_OCCHO_VIEW));
        borrarTestsMenuItem.setText(Messages.getString("borrarTestsMenuItem.text", Messages.BUNDLE_NAME_OCCHO_VIEW));
        crearTestMenuItem.setText(Messages.getString("crearTestMenuItem.text", Messages.BUNDLE_NAME_OCCHO_VIEW));
        englishMenuItem.setText(Messages.getString("englishMenuItem.text", Messages.BUNDLE_NAME_OCCHO_VIEW));
        lenguajesMenu.setText(Messages.getString("lenguajesMenu.text", Messages.BUNDLE_NAME_OCCHO_VIEW));
        loginMenuItem.setText(Messages.getString("loginMenuItem.text", Messages.BUNDLE_NAME_OCCHO_VIEW));
        logoutMenuItem.setText(Messages.getString("logoutMenuItem.text", Messages.BUNDLE_NAME_OCCHO_VIEW));
        profesoresMenu.setText(Messages.getString("profesoresMenu.text", Messages.BUNDLE_NAME_OCCHO_VIEW));
        profesoresMenuItem.setText(Messages.getString("profesoresMenuItem.text", Messages.BUNDLE_NAME_OCCHO_VIEW));
        realizarTestMenuItem.setText(Messages.getString("realizarTestMenuItem.text", Messages.BUNDLE_NAME_OCCHO_VIEW));
        registrarMenuItem.setText(Messages.getString("registrarMenuItem.text", Messages.BUNDLE_NAME_OCCHO_VIEW));
        spanishMenuItem.setText(Messages.getString("spanishMenuItem.text", Messages.BUNDLE_NAME_OCCHO_VIEW));
        verResultadosMenuItem.setText(Messages.getString("verResultadosMenuItem.text", Messages.BUNDLE_NAME_OCCHO_VIEW));
        logMenuItem.setText(Messages.getString("logMenuItem.text", Messages.BUNDLE_NAME_OCCHO_VIEW));
        javax.swing.JMenu fileMenu = menuBar.getMenu(0);
        javax.swing.JMenu helpMenu = menuBar.getMenu(5);
        fileMenu.setText(Messages.getString("fileMenu.text", Messages.BUNDLE_NAME_OCCHO_VIEW));
        helpMenu.setText(Messages.getString("helpMenu.text", Messages.BUNDLE_NAME_OCCHO_VIEW));
        verResultadosProfesorMenuItem.setText(Messages.getString("verResultadosProfesorMenuItem.text", Messages.BUNDLE_NAME_OCCHO_VIEW));
        for (Component c : mdiPanel.getComponents()) {
            if (c instanceof Internacionalizable) {
                Internacionalizable i = (Internacionalizable) c;
                i.setLenguaje();
            }
        }
    }
    
    private void setOpcionesAdmin(boolean activado) {
        administradorMenu.setEnabled(activado);
    }
    
    private void setOpcionesProfesor(boolean activado) {
        profesoresMenu.setEnabled(activado);
    }
    
    private void setOpcionesAlumno(boolean activado) {
        alumnosMenu.setEnabled(activado);
    }
}
